/**
 *
 * 用于存放公共的功能函数
 *
 */
import moment from 'moment/moment';
/**
 * 生成query字符串
 * @param obj
 * @returns {string}
 */
export function genQuery(obj) {
  let index = 0;
  let query = '';
  Object.keys(obj).forEach((name) => {
    if (obj[name] !== undefined && obj[name] !== null) {
      if (index !== 0) {
        query += '&';
      }
    //  const value = obj[name];
      const value = typeof obj[name] === 'string' ? encodeURIComponent(obj[name]) : obj[name];
      query += `${name}=${value}`;
      index += 1;
    }
  });
  return query;
}

/**
 * @param name 希望获取的query项
 * @returns 对应的query值
 */
export function getQueryString(name) {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i');
  const r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

/**
 * 数组去重
 * @param arr
 * @returns {Array}
 */
export function unique(arr) {
  const newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (newArr.indexOf(arr[i]) === -1) {
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

/**
 * 生成UUid
 * @returns {string}
 */
/* eslint-disable */
export function generateUUID() {
  let d = new Date().getTime();
  const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    let r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d/16);
    return (c=='x' ? r : (r&0x7|0x8)).toString(16);
  });
  return uuid;
}
/* eslint-enable */

// 验证手机号
export function chaeckMobilePhone(linkPhone) {
  const regphone = /^1[0-9]{10}$/;
  if (!regphone.test(linkPhone.trim())) {
    return false;
  }
  return true;
}
// 验证固定电话（3位区号，4位区号）
export function checkLandLineTel(linkPhone) {
  const threePhone = /^(0[0-9]{2})+([0-9][0-9]{7})+$/;
  const fourPhone = /^(0[0-9]{3})+([0-9][0-9]{6,7})+$/;
  if (threePhone.test(linkPhone.trim()) || fourPhone.test(linkPhone.trim())) {
    return true;
  }
  return false;
}

const TIME_FORMAT = 'YYYY/MM/DD';
export function formatUtcTime(timeSecond, dstFormat) {
  const outformat = dstFormat || TIME_FORMAT;
  return moment(timeSecond).format(outformat);
}

// 坐标 火星---->地球
const pi = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
function transformLat(x, y) {
  let ret = -100.0 + (2.0 * x) + (3.0 * y) + (0.2 * y * y) + (0.1 * x * y)
    + (0.2 * Math.sqrt(Math.abs(x)));
  ret += (((20.0 * Math.sin(6.0 * x * pi)) + (20.0 * Math.sin(2.0 * x * pi))) * 2.0) / 3.0;
  ret += (((20.0 * Math.sin(y * pi)) + (40.0 * Math.sin((y / 3.0) * pi))) * 2.0) / 3.0;
  ret += (((160.0 * Math.sin((y / 12.0) * pi)) + (320 * Math.sin((y * pi) / 30.0))) * 2.0) / 3.0;
  return ret;
}
function transformLon(x, y) {
  let ret = 300.0 + x + (2.0 * y) + (0.1 * x * x) + (0.1 * x * y) + (0.1 * Math.sqrt(Math.abs(x)));
  ret += (((20.0 * Math.sin(6.0 * x * pi)) + (20.0 * Math.sin(2.0 * x * pi))) * 2.0) / 3.0;
  ret += (((20.0 * Math.sin(x * pi)) + (40.0 * Math.sin((x / 3.0) * pi))) * 2.0) / 3.0;
  ret += (((150.0 * Math.sin((x / 12.0) * pi)) + (300.0 * Math.sin((x / 30.0) * pi))) * 2.0) / 3.0;
  return ret;
}
function transform(lat, lon) {
  let dLat = transformLat(lon - 105.0, lat - 35.0);
  let dLon = transformLon(lon - 105.0, lat - 35.0);
  const radLat = (lat / 180.0) * pi;
  let magic = Math.sin(radLat);
  magic = 1 - (ee * magic * magic);
  const sqrtMagic = Math.sqrt(magic);
  dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi);
  dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * pi);
  const mgLat = lat + dLat;
  const mgLon = lon + dLon;
  return {
    lat: mgLat,
    lng: mgLon
  };
}
export function GCJ_TO_GPS84(lat, lon) {
  const gps = transform(lat, lon);
  const lontitude = (lon * 2) - gps.lng;
  const latitude = (lat * 2) - gps.lat;
  return {
    lng: lontitude,
    lat: latitude
  };
}
